package com.platform.config;

import java.io.IOException;

import javax.sql.DataSource;

import org.activiti.engine.DynamicBpmnService;
import org.activiti.engine.FormService;
import org.activiti.engine.HistoryService;
import org.activiti.engine.IdentityService;
import org.activiti.engine.ManagementService;
import org.activiti.engine.ProcessEngine;
import org.activiti.engine.RepositoryService;
import org.activiti.engine.RuntimeService;
import org.activiti.engine.TaskService;
import org.activiti.spring.SpringProcessEngineConfiguration;
import org.activiti.spring.boot.ProcessEngineConfigurationConfigurer;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.core.io.Resource;
import org.springframework.core.io.ResourceLoader;
import org.springframework.core.io.support.PathMatchingResourcePatternResolver;
import org.springframework.transaction.PlatformTransactionManager;

/**
 * 工作流配置
 *
 * @author 肖亮
 */
@Configuration
public class ActivitiConfig implements ProcessEngineConfigurationConfigurer {

	/**
	 * 解決工作流生成图片乱码问题
	 *
	 * @param processEngineConfiguration
	 *            processEngineConfiguration
	 */
	@Override
	public void configure(SpringProcessEngineConfiguration processEngineConfiguration) {
		processEngineConfiguration.setActivityFontName("宋体");
		processEngineConfiguration.setAnnotationFontName("宋体");
		processEngineConfiguration.setLabelFontName("宋体");
	}

	/**
	 * 核心流程引擎类
	 *
	 * @param transactionManager
	 *            transactionManager
	 * @param dataSource
	 *            dataSource
	 * @return ProcessEngine
	 */
	@Bean
	public ProcessEngine processEngine(PlatformTransactionManager transactionManager, DataSource dataSource)
			throws IOException {
		SpringProcessEngineConfiguration processEngineConfiguration = new SpringProcessEngineConfiguration();
		// 自动部署已有的流程文件
		Resource[] resources = new PathMatchingResourcePatternResolver()
				.getResources(ResourceLoader.CLASSPATH_URL_PREFIX + "processes1/*.bpmn");
		processEngineConfiguration.setDeploymentResources(resources);
		processEngineConfiguration.setTransactionManager(transactionManager);
		
		
		processEngineConfiguration.setDataSource(dataSource);
		processEngineConfiguration.setDatabaseSchemaUpdate("true");

		return processEngineConfiguration.buildProcessEngine();
	}

	/**
	 * 流程仓库Service，用于管理流程仓库，例如部署、删除、读取流程资源
	 *
	 * @param processEngine
	 *            processEngine
	 * @return RepositoryService
	 */
	@Bean
	public RepositoryService repositoryService(ProcessEngine processEngine) {
		return processEngine.getRepositoryService();
	}

	/**
	 * 运行时Service，可以也拿过来处理所有正在运行状态的流程实例、任务等
	 *
	 * @param processEngine
	 *            processEngine
	 * @return RuntimeService
	 */
	@Bean
	public RuntimeService runtimeService(ProcessEngine processEngine) {
		return processEngine.getRuntimeService();
	}

	/**
	 * 任务Service，用于管理和查询任务，例如签收、办理、指派等
	 *
	 * @param processEngine
	 *            processEngine
	 * @return TaskService
	 */
	@Bean
	public TaskService taskService(ProcessEngine processEngine) {
		return processEngine.getTaskService();
	}

	/**
	 * 历史Service，用于查询所有历史数据，例如流程实例、任务、活动、变量、附件
	 *
	 * @param processEngine
	 *            processEngine
	 * @return HistoryService
	 */
	@Bean
	public HistoryService historyService(ProcessEngine processEngine) {
		return processEngine.getHistoryService();
	}

	/**
	 * 引擎管理Service，和具体业务无关，主要可以查询引擎配置、数据库、作业等
	 *
	 * @param processEngine
	 *            processEngine
	 * @return ManagementService
	 */
	@Bean
	public ManagementService managementService(ProcessEngine processEngine) {
		return processEngine.getManagementService();
	}

	/**
	 * 身份Service，用于管理和查询用户、组之间的关系
	 *
	 * @param processEngine
	 *            processEngine
	 * @return IdentityService
	 */
	@Bean
	public IdentityService identityService(ProcessEngine processEngine) {
		return processEngine.getIdentityService();
	}

	/**
	 * 表单Service，用于读取流程、任务相关的表单数据
	 *
	 * @param processEngine
	 *            processEngine
	 * @return FormService
	 */
	@Bean
	public FormService formService(ProcessEngine processEngine) {
		return processEngine.getFormService();
	}

	/**
	 * 一个新增的服务，用于动态修改流程中的一些参数信息等，是引擎中的一个辅助的服务
	 *
	 * @param processEngine
	 *            processEngine
	 * @return DynamicBpmnService
	 */
	@Bean
	public DynamicBpmnService dynamicBpmnService(ProcessEngine processEngine) {
		return processEngine.getDynamicBpmnService();
	}
}
